<template>
  <div :style="'width:' + width">
    <el-form :label-width="labelWidth" :disabled="disabled" label-position="left">
      <el-form-item :label="name" :required="required">
        <div style="display: flex">
          <el-select style="flex: 1" v-model="data.typeId" placeholder="请选择险种" :remote-method="getInsureTypes"
            :loading="loading" @input="input">
            <el-option v-for="item in types" :key="item.ISTY_ID" :label="item.ISTY_Name"
              :value="item.ISTY_ID"></el-option>
          </el-select>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getInsureTypes } from '@/api/flowCenter/field/insureTypeSelection'
export default {
  props: ['value', 'disabled', 'props', 'required', 'isBusiness'],
  data: function () {
    return {
      data: {
        typeId: null,
        displayName: '',
      },
      types: [],
      loading: false,
    }
  },
  computed: {
    myprops() {
      return this.props ? this.props : []
    },
    name() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'name')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '险种'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '险种'
        }
      }
      return '险种'
    },
    labelWidth() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'labelWidth')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '140px'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '140px'
        }
      }
      return '140px'
    },
    width() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'width')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '100%'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '100%'
        }
      }
      return '100%'
    },
  },
  methods: {
    getInsureTypes() {
      this.loading = true
      getInsureTypes().then(({ data }) => {
        this.types = data
        this.loading = false
      })
    },
    input(val) {
      this.data.typeId = val
      let type = this.types.find((x) => x.ISTY_ID == this.data.typeId)
      this.data.displayName = type.ISTY_Name
      this.$emit('input', JSON.stringify(this.data))
      this.$emit('display', this.data.displayName)
    },
  },
  mounted: function () {
    this.getInsureTypes()
    if (this.value) {
      this.data = JSON.parse(this.value)
    }
  },
}
</script>
<style scoped>
.el-form-item {
  margin: 10px !important;
}
</style>